<template>
  <div class="password-wrap">
    <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        label-width="120px"
        class="demo-ruleForm"
    >
      <el-form-item label="原密码" prop="oldPassword">
        <el-input v-model="ruleForm.oldPassword" maxlength="20" type="password" autocomplete="off"/>
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
            maxlength="20"
            v-model="ruleForm.newPassword"
            type="password"
            autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="新密码确认" prop="checkPassword">
        <el-input
            maxlength="20"
            v-model="ruleForm.checkPassword"
            type="password"
            autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button v-loading="loading" type="primary" @click="submitForm(ruleFormRef)"
        >提交
        </el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup lang="ts">
import $api from '@/api/index'

const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else {
    callback()
  }
}

const validatePass2 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入新密码'))
  } else if (value.length < 6) {
    callback(new Error('至少6个字符'))
  } else {
    if (ruleForm.checkPassword !== '') {
      if (!ruleFormRef.value) return
      ruleFormRef.value.validateField('checkPassword', () => null)
    }
    callback()
  }
}

const validatePass3 = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请再次输入密码'))
  } else if (value !== ruleForm.newPassword) {
    callback(new Error("两次密码不匹配"))
  } else {
    callback()
  }
}


import {reactive, ref} from 'vue'
import type {FormInstance, FormRules} from 'element-plus'
import {ElNotification} from "element-plus";

const ruleFormRef = ref<FormInstance>()

const ruleForm = reactive({
  oldPassword: '',
  newPassword: '',
  checkPassword: '',
})
const loading = ref(false)
const rules = reactive<FormRules>({
  oldPassword: [{validator: validatePass, trigger: 'blur'}],
  newPassword: [{validator: validatePass2, trigger: 'blur'}],
  checkPassword: [{validator: validatePass3, trigger: 'blur'}],
})

const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      submit()
    } else {
      console.log('error submit!')
      return false
    }
  })
}

const submit = () => {
  if (loading.value) {
    return
  }
  loading.value = true
  $api.userApi.changePassword(ruleForm).then(res => {
    loading.value = false
    ElNotification({
      title: '成功',
      message: '修改成功！',
      type: 'success',
    })
    ruleForm.checkPassword = ''
    ruleForm.oldPassword = ''
    ruleForm.newPassword = ''
  }).catch(() => {
    loading.value = false
  })
}

</script>

<style scoped lang="scss">
.password-wrap {
  width: 100%;
  max-width: 400px;
  padding-top: 40px;
}

</style>